/**
 * May 6, 2014 4:05:49 PM
 */
package elitech.vietnam.copyweatherfashion.adapters;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment.ConfirmDialogClick;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment.Type;
import elitech.vietnam.copyweatherfashion.dialog.WarningDialogFragment;
import elitech.vietnam.copyweatherfashion.entities.Member;
import elitech.vietnam.copyweatherfashion.fragments.MemberPageFragment;
import elitech.vietnam.copyweatherfashion.fragments.settings.FriendListFragment;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 *
 */
public class FriendListAdapter extends ArrayAdapter<Member> {
	
	String mTag;
	MainActivity mActivity;

	public FriendListAdapter(Context context, int resource, List<Member> objects, String tag) {
		super(context, resource, objects);
		mActivity = (MainActivity) context;
		mTag = tag;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;
		
		if (row == null) {
			row = LayoutInflater.from(getContext()).inflate(R.layout.settings_friendlistitem, parent, false);
			
			holder = new ViewHolder();
			holder.mAvatar = (ImageView) row.findViewById(R.id.frienditem_imgAvatar);
			holder.mName = (TextView) row.findViewById(R.id.frienditem_txtName);
			holder.mStatus = (TextView) row.findViewById(R.id.frienditem_txtStatus);
			holder.mDelete = (Button) row.findViewById(R.id.frienditem_btnDelete);
			
			if (mTag.equals(FriendListFragment.TAG_FOLLOWING))
				holder.mDelete.setVisibility(View.GONE);
			
			row.setTag(holder);
		} else 
			holder = (ViewHolder) row.getTag();
		
		final Member mMember = getItem(position);
		final MainActivity mActivity = (MainActivity) getContext();
		UrlImageViewHelper.setUrlDrawable(holder.mAvatar, Const.SERVER_IMAGE_THUMB_URL + mMember.Image, R.drawable.avatar);
		holder.mName.setText((mMember.Name.length()==0) ? mMember.Username : mMember.Name);
		holder.mStatus.setText(mMember.Profile);
		
		holder.mClick = new ConfirmDialogClick() {
			@Override
			public void yesClick() {
				new AsyncTask<Integer, Integer, Boolean>() {
					@Override
					protected void onPreExecute() {
						holder.mDelete.setEnabled(false);
					}
					@Override
					protected Boolean doInBackground(Integer... params) {
						try {
							return new Service().unFollow(mActivity.mUser.Id, mMember.Id) > 0;
						} catch (IOException | XmlPullParserException e) {
							return false;
						}
					}
					@Override
					protected void onPostExecute(Boolean result) {
						holder.mDelete.setEnabled(true);
						if (!result) {
							new WarningDialogFragment.Builder()
							.setMessage(mActivity.getString(R.string.cannot_connect_to_server))
							.build().show(mActivity.getSupportFragmentManager());
						} else {
							Toast.makeText(mActivity, R.string.setting_delete_friend_success, Toast.LENGTH_SHORT).show();
							remove(mMember);
							notifyDataSetChanged();
						}
					}
				}.execute();
			}
			@Override
			public void noClick() {
			}
		};
		
		holder.mDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new ConfirmDialogFragment.Builder()
				.setMessage(((mMember.Name.length()==0) ? mMember.Username : mMember.Name) + " " + mActivity.getString(R.string.setting_delete_friend_message_confirm))
				.setTargetFragment(holder)
				.setType(Type.FRAGMENT)
				.build().show(mActivity.getSupportFragmentManager());
			}
		});
		
		row.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MemberPageFragment fragment = new MemberPageFragment();
				Bundle bundle = new Bundle();
				bundle.putString(MemberPageFragment.ARG_MEMBER, new Gson().toJson(mMember));
				fragment.setArguments(bundle);
				mActivity.getCurrentBase().replaceFragment(fragment, true);
			}
		});
		
		return row;
	}
	
	public static class ViewHolder extends Fragment implements ConfirmDialogClick {
		ImageView mAvatar;
		TextView mName, mStatus;
		Button mDelete;
		ConfirmDialogClick mClick;
		
		public ViewHolder() {
		}

		@Override
		public void yesClick() {
			if (mClick != null)
				mClick.yesClick();
		}
		@Override
		public void noClick() {
		}
	}
}
